﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Text.RegularExpressions;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace RM.WebClient
{
    public static class ValidationFunctions
    {
        public static void SetValidation(this FrameworkElement frameworkElement, string message)
        {
            CustomValidation customValidation = new CustomValidation(message);

            Binding binding = new Binding("ValidationError")
            {
                Mode = System.Windows.Data.BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true,
                Source = customValidation
            };
            frameworkElement.SetBinding(Control.TagProperty, binding);
        }

        public static void RaiseValidationError(this FrameworkElement frameworkElement)
        {
            BindingExpression b = frameworkElement.GetBindingExpression(Control.TagProperty);

            if (b != null)
            {
                ((CustomValidation)b.DataItem).ShowErrorMessage = true;
                b.UpdateSource();
            }
        }

        public static void ClearValidationError(this FrameworkElement frameworkElement)
        {
            BindingExpression b = frameworkElement.GetBindingExpression(Control.TagProperty);

            if (b != null)
            {
                ((CustomValidation)b.DataItem).ShowErrorMessage = false;
                b.UpdateSource();
            }
        }

        public static bool IsNumberValid(this FrameworkElement frameworkElement)
        {
            frameworkElement.ClearValidationError();
            bool isNumberValid = true;
            long number;
            TextBox textBox = frameworkElement as TextBox;
            long.TryParse(textBox.Text, out number);
            if (number == 0)
            {
                textBox.SetValidation("Please enter Non Zero numeric value");
                textBox.RaiseValidationError();
                isNumberValid = false;
            }
            return isNumberValid;
        }

        public static bool ValidateFixLength(this FrameworkElement frameworkElement, int length)
        {
            frameworkElement.ClearValidationError();
            bool isLengthValid = true;
            TextBox textBox = frameworkElement as TextBox;
            if (!textBox.Text.Length.Equals(length))
            {
                textBox.SetValidation("The length of this field should be exactly " + length.ToString());
                textBox.RaiseValidationError();
                isLengthValid = false;
            }
            return isLengthValid;
        }

        public static bool IsEmailValid(this FrameworkElement frameworkElement)
        {
            frameworkElement.ClearValidationError();
            bool isEmailValid = true;
            string emailExpression = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
            Regex re = new Regex(emailExpression);
            TextBox textBox = frameworkElement as TextBox;
            if (!re.IsMatch(textBox.Text))
            {
                textBox.SetValidation("Please enter a valid Email Address");
                textBox.RaiseValidationError();
                isEmailValid = false;
            }
            return isEmailValid;
        }

        public static void IsResumeUploaded(this FrameworkElement frameworkElement)
        {
            frameworkElement.ClearValidationError();
            TextBox textBox = frameworkElement as TextBox;
            textBox.SetValidation("Please upload a resume or copy and paste resume here");
            textBox.RaiseValidationError();
        }

        public static bool validateField(this FrameworkElement frameworkElement, string sDisplayName)
        {
            frameworkElement.ClearValidationError();
            String sControlType = frameworkElement.GetType().ToString();
            bool b = true;

            if (sControlType.Contains("TextBox"))
            {
                TextBox textBox = frameworkElement as TextBox;
                if (textBox.Text == "")
                {
                    textBox.SetValidation("Please enter " + sDisplayName);
                    textBox.RaiseValidationError();
                    b = false;
                }
            }

            else if (sControlType.Contains("DatePicker"))
            {
                DatePicker datePicker = frameworkElement as DatePicker;
                if (datePicker.Text == "")
                {
                    datePicker.SetValidation("Please enter " + sDisplayName);
                    datePicker.RaiseValidationError();
                    b = false;
                }
            }

            else if (sControlType.Contains("ComboBox"))
            {
                ComboBox comboBox = frameworkElement as ComboBox;
                if (comboBox.SelectedIndex == -1)
                {
                    comboBox.SetValidation("Please select a value from " + sDisplayName);
                    comboBox.RaiseValidationError();
                    b = false;
                }
            }

            return b;
        }
    }

    public class CustomValidation
    {
        #region Private Members
        private string message;
        #endregion

        #region Properties
        public bool ShowErrorMessage
        {
            get;
            set;
        }

        public object ValidationError
        {
            get
            {
                return null;
            }
            set
            {
                if (ShowErrorMessage)
                {
                    throw new ValidationException(message);
                }
            }
        }
        #endregion

        #region Constructor
        public CustomValidation(string message)
        {
            this.message = message;
        }
        #endregion
    }

}
